<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:gml="http://www.opengis.net/gml" 
	xmlns:ogr="http://ogr.maptools.org/"
	xmlns:ramon_geo="http://ec.europa.eu/eurostat/ramon/ontologies/geographic.rdf#" 
	xmlns:ramon="http://rdfdata.eionet.europa.eu/ramon/ontology/"
	xmlns:fao="http://aims.fao.org/aos/geopolitical.owl#"
	xmlns:owl="http://www.w3.org/2002/07/owl#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

<xsl:output method="text" indent="yes"/>	
<xsl:template match="/ogr:FeatureCollection">

	<xsl:variable name="dbpedia_dump" select="document('../sources/dbpedia/nuts_dbpedia_settlements_over_10000.xml')"/>
	<xsl:variable name="nuts_dump" select="document('../sources/ramon/nuts2008.rdf')"/>
	<xsl:variable name="eionet_dump" select="document('../sources/eionet/eionet.rdf')"/>	
	<xsl:variable name="fao_dump" select="document('../sources/fao/geopolitical.owl')"/>	
	<xsl:variable name="ukos_mappings" select="document('../sources/ukos/ukos_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="geoes_mappings" select="document('../sources/geoes/geoes_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="touches_relations" select="document('../sources/touches.xml')"/>	

	<xsl:result-document href="NUTS.ttl">
@prefix geo: &lt;http://www.w3.org/2003/01/geo/wgs84_pos#&gt; .
@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .
@prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt; .
@prefix nuts: &lt;http://nuts.geovocab.org/id/&gt; .
@prefix ramon: &lt;http://rdfdata.eionet.europa.eu/ramon/ontology/&gt; .
@prefix ngeo: &lt;http://geovocab.org/geometry#&gt; .
@prefix spatial: &lt;http://geovocab.org/spatial#&gt; .
@prefix owl: &lt;http://www.w3.org/2002/07/owl#&gt; .
@prefix dc: &lt;http://purl.org/dc/elements/1.1/&gt; .

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >
	
		<xsl:variable name="nuts_id" select="ogr:NUTS_ID"/>
		<xsl:variable name="nuts_uri" select="concat('http://nuts.geovocab.org/id/',$nuts_id)"/>
		<xsl:variable name="eionet_uri" select="concat('http://rdfdata.eionet.europa.eu/eea/countries/',$nuts_id)"/>
		<xsl:variable name="nuts_node" select="$nuts_dump/rdf:RDF/ramon_geo:NUTSRegion[@rdf:about=$nuts_id]"/>
		<xsl:variable name="eionet_node" select="$eionet_dump/rdf:RDF/rdf:Description[@rdf:about=$eionet_uri]"/>
		<xsl:variable name="fao_node" select="$fao_dump/rdf:RDF/fao:self_governing[fao:codeISO2=$nuts_id]"/>

		<xsl:apply-templates select="ogr:geometryProperty">
			<xsl:with-param name="dbpedia_nodes" select="$dbpedia_dump/table/row[column[position()=1]=$nuts_uri]" />
			<xsl:with-param name="nuts_node" select="$nuts_node" />
			<xsl:with-param name="eionet_node" select="$eionet_node" />
			<xsl:with-param name="fao_node" select="$fao_node" />
			<xsl:with-param name="ukos_node" select="$ukos_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
			<xsl:with-param name="geoes_node" select="$geoes_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
			<xsl:with-param name="touches_nodes" select="$touches_relations/table/row[column[position()=1]=$nuts_uri]" />
			<xsl:with-param name="nuts_id" select="$nuts_id" />
		</xsl:apply-templates>

nuts:<xsl:value-of select="ogr:NUTS_ID"/>_geometry dc:rights "© EuroGeographics for the administrative boundaries." .
		<xsl:apply-templates select="ogr:geometryProperty/child::node()">
			<xsl:with-param name="geometry_id" select="concat('nuts:',ogr:NUTS_ID,'_geometry')" />
		</xsl:apply-templates>

	</xsl:for-each>
	</xsl:result-document>

</xsl:template>

<xsl:template name="geometry" match="ogr:geometryProperty">
	<xsl:param name="dbpedia_nodes" />
	<xsl:param name="nuts_node" />
	<xsl:param name="eionet_node" />
	<xsl:param name="fao_node" />
	<xsl:param name="ukos_node" />
	<xsl:param name="geoes_node" />
	<xsl:param name="touches_nodes" />
	<xsl:param name="nuts_id" />

	<xsl:variable name="geometry_id" select="concat('nuts:',$nuts_id,'_geometry')"/>

nuts:<xsl:value-of select="$nuts_id" /> rdf:type ramon:NUTSRegion, spatial:Feature .
nuts:<xsl:value-of select="$nuts_id" /> rdfs:label "<xsl:value-of select="$nuts_node/rdfs:label" />" .
nuts:<xsl:value-of select="$nuts_id" /> ramon:name "<xsl:value-of select="$nuts_node/ramon_geo:name" />" .
nuts:<xsl:value-of select="$nuts_id" /> ramon:level "<xsl:value-of select="$nuts_node/ramon_geo:level" />"^^&lt;http://www.w3.org/2001/XMLSchema#integer&gt; .
nuts:<xsl:value-of select="$nuts_id" /> ramon:code "<xsl:value-of select="$nuts_node/ramon_geo:code" />" .
nuts:<xsl:value-of select="$nuts_id" /> ngeo:geometry <xsl:value-of select="$geometry_id" /> .
<xsl:if test="$nuts_node/ramon_geo:hasParentRegion">
nuts:<xsl:value-of select="$nuts_id" /> spatial:PP nuts:<xsl:value-of select="$nuts_node/ramon_geo:hasParentRegion/@rdf:resource" /> .
</xsl:if>
<xsl:if test="$fao_node">
<xsl:for-each select='$fao_node/fao:hasBorderWith' >
<xsl:variable name="ec_node" select="substring-after(./@rdf:resource, '#')"/>
<xsl:variable name="codeISO2" select="../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2"/>
<xsl:if test="$nuts_node/../ramon_geo:NUTSRegion[@rdf:about=$codeISO2]">
nuts:<xsl:value-of select="$nuts_id" /> spatial:EC nuts:<xsl:value-of select="$codeISO2" /> .
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:if test="$dbpedia_nodes">
<xsl:for-each select='$dbpedia_nodes' >
nuts:<xsl:value-of select="$nuts_id" /> spatial:PPi &lt;<xsl:value-of select="column[position()=last()]"/>&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$touches_nodes">
<xsl:for-each select='$touches_nodes' >
nuts:<xsl:value-of select="$nuts_id" /> spatial:EC &lt;<xsl:value-of select="column[position()=last()]"/>&gt; .
</xsl:for-each>
</xsl:if>
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://rdfdata.eionet.europa.eu/ramon/nuts2008/',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://ec.europa.eu/eurostat/ramon/rdfdata/nuts2008/',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://estatwrap.ontologycentral.com/dic/geo#',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://nuts.psi.enakting.org/id/',$nuts_id)" />&gt; .
<xsl:if test="$eionet_node">
<xsl:for-each select='$eionet_node/owl:sameAs' >
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$ukos_node">
<xsl:for-each select='$ukos_node/rdfs:seeAlso' >
nuts:<xsl:value-of select="$nuts_id" /> rdfs:seeAlso &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$geoes_node">
<xsl:for-each select='$geoes_node/rdfs:seeAlso' >
nuts:<xsl:value-of select="$nuts_id" /> rdfs:seeAlso &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>

</xsl:template>


<xsl:template match="gml:MultiPolygon|gml:MultiSurface">
	<xsl:param name="geometry_id" />
<xsl:value-of select="$geometry_id" /> rdf:type ngeo:MultiPolygon .
	<xsl:apply-templates select="child::gml:polygonMember|child::gml:surfaceMember">
		<xsl:with-param name="geometry_id" select="$geometry_id" />
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:polygonMember|gml:surfaceMember">
	<xsl:param name="geometry_id" />
	<xsl:variable name="member_id" select="concat('_:',generate-id())"/>
<xsl:value-of select="$geometry_id" /> ngeo:polygonMember <xsl:value-of select="$member_id"/> .
	<xsl:apply-templates select="child::gml:Polygon|child::gml:Surface">
		<xsl:with-param name="geometry_id" select="$member_id" />
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:Polygon|gml:Surface">
	<xsl:param name="geometry_id" />
<xsl:value-of select="$geometry_id" /> rdf:type ngeo:Polygon .
	<xsl:apply-templates select="gml:interior|gml:innerBoundaryIs|gml:exterior|gml:outerBoundaryIs">
		<xsl:with-param name="geometry_id" select="$geometry_id" />
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:interior|gml:innerBoundaryIs">
	<xsl:param name="geometry_id" />
	<xsl:variable name="bnode_id" select="concat('_:',generate-id(gml:LinearRing))"/>
<xsl:value-of select="$geometry_id" /> ngeo:interior <xsl:value-of select="$bnode_id"/> .
	<xsl:apply-templates select="child::gml:LinearRing">
		<xsl:with-param name="geometry_id" select="$geometry_id" />
		<xsl:with-param name="bnode_id" select="$bnode_id" />
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:exterior|gml:outerBoundaryIs">
	<xsl:param name="geometry_id" />
	<xsl:variable name="bnode_id" select="concat('_:',generate-id(gml:LinearRing))"/>
<xsl:value-of select="$geometry_id" /> ngeo:exterior <xsl:value-of select="$bnode_id"/> .
	<xsl:apply-templates select="child::gml:LinearRing">
		<xsl:with-param name="geometry_id" select="$geometry_id" />
		<xsl:with-param name="bnode_id" select="$bnode_id" />
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:LinearRing">
	<xsl:param name="geometry_id" />
	<xsl:param name="bnode_id" />
	
<xsl:value-of select="$bnode_id"/> rdf:type ngeo:LinearRing .
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates">
		<xsl:with-param name="geometry_id" select="$bnode_id"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:LineString|gml:Curve">
	<xsl:param name="geometry_id" />
<xsl:value-of select="$geometry_id"/> rdf:type ngeo:LineString .
	<xsl:apply-templates select="child::gml:posList|child::gml:coordinates">
		<xsl:with-param name="geometry_id" select="concat('_:',generate-id())"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="gml:posList|gml:coordinates">
	<xsl:param name="geometry_id" />
<xsl:value-of select="$geometry_id"/> ngeo:posList ( 
	
	<xsl:call-template name="format-posList">
        <xsl:with-param name="text" select="self::node()"/>
    </xsl:call-template>
	
) .
</xsl:template>

<xsl:template name="format-posList">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="not(contains($text, ','))">
      <xsl:value-of select="$text"/>
    </xsl:when>
    <xsl:otherwise>
      [ geo:long "<xsl:value-of select="substring-before($text, ',')"/>"; 
      <xsl:call-template name="format-posList-close">
        <xsl:with-param name="text" select="substring-after($text, ',')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="format-posList-close">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="not(contains($text, ' '))">
      geo:lat "<xsl:value-of select="$text"/>" ]
    </xsl:when>
    <xsl:otherwise>
      geo:lat "<xsl:value-of select="substring-before($text, ' ')"/>" ]
      <xsl:call-template name="format-posList">
        <xsl:with-param name="text" select="substring-after($text, ' ')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>